package com.whut.monitor.controller;

import com.whut.monitor.vo.Grid;
import com.whut.monitor.vo.PageFilter;
import com.whut.monitor.vo.ResponseMsg;
import com.whut.monitor.vo.Tree;
import com.whut.monitor.netty.handler.ModelUpdateInboundHandler;
import com.whut.monitor.service.ICompensatorService;
import com.whut.monitor.vo.CompensatorVo;
import org.apache.commons.io.FileUtils;
import org.apache.commons.io.FilenameUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author CFlower
 * 2017/4/5
 * 补偿器管理接口
 */
@Controller
@RequestMapping("/compensator")
public class CompensatorController {
    public static Map<String, String> filePathMap = new HashMap<>();
    @Autowired
    private ICompensatorService compensatorService;
    @Resource(name = "ModelUpdateInboundHandler")
    private ModelUpdateInboundHandler modelUpdateInboundHandler;

    @RequestMapping("/manager")
    public String manager() {
        return "/mintor/basicInformation/compensator";
    }

    @RequestMapping("/dataGrid")
    @ResponseBody
    public Grid dataGrid(CompensatorVo compensator, PageFilter pageFilter) {
        Grid grid = new Grid();
        grid.setRows(compensatorService.dataGrid(compensator, pageFilter));
        grid.setTotal(compensatorService.count(compensator, pageFilter));
        return grid;
    }

    @RequestMapping("/add")
    @ResponseBody
    public ResponseMsg add(CompensatorVo compensatorVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorService.add(compensatorVo);
            j.setSuccess(true);
            j.setMsg("添加成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return j;
    }

    @RequestMapping("/edit")
    @ResponseBody
    public ResponseMsg edit(CompensatorVo compensatorVo) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorService.edit(compensatorVo);
            j.setSuccess(true);
            j.setMsg("编辑成功！");
        } catch (Exception e) {
            j.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return j;
    }

    @RequestMapping("/delete")
    @ResponseBody
    public ResponseMsg delete(Integer id) {
        ResponseMsg j = new ResponseMsg();
        try {
            compensatorService.delete(id);
            j.setMsg("删除成功！");
            j.setSuccess(true);
        } catch (Exception e) {
            j.setMsg(e.getMessage());
            e.printStackTrace();
        }
        return j;
    }

    @RequestMapping("/tree")
    @ResponseBody
    public List<Tree> tree() {
        return compensatorService.tree();
    }

    @RequestMapping("/uploadbin")
    @ResponseBody
    public ResponseMsg uploadbin(@RequestParam MultipartFile file, HttpServletRequest request) {
        ResponseMsg j = new ResponseMsg();
        if (file.isEmpty()) {
            j.setMsg("请选择要上传的文件！");
        } else {
            String originalFilename = file.getOriginalFilename();
            String fileBaseName = FilenameUtils.getBaseName(originalFilename);
            Date now = new Date();
            // 可以方便地修改日期格式
            SimpleDateFormat dateFormat = new SimpleDateFormat("yyyyMMddHHmmss");
            String dataStr = dateFormat.format(now);
            String floderName = fileBaseName + "_" + dataStr;
            try {
                String genePicPath = request.getSession().getServletContext().getRealPath("/upload/" + floderName);
                // 返回给补偿器的地址
                String realPath = genePicPath + "/" + originalFilename;
                filePathMap.put("filePath", realPath);
                // 把上传的文件放到服务器的文件夹下
                FileUtils.copyInputStreamToFile(file.getInputStream(), new File(genePicPath, originalFilename));
                //coding
                j.setSuccess(true);
                j.setMsg("上传成功！");
            } catch (Exception e) {
                j.setMsg(e.getMessage());
                e.printStackTrace();
            }
        }
        return j;
    }

    @RequestMapping("/updatemodel")
    @ResponseBody
    public ResponseMsg updatemodel(String updateorder, String cptNum) throws Exception {
        ResponseMsg j = new ResponseMsg();
        String udBinOrder = "SRet_bin";
        if (updateorder.equals(udBinOrder)) {
            if (cptNum.isEmpty()) {
                j.setSuccess(false);
                j.setMsg("补偿器编号不能为空");
                return j;
            }
            modelUpdateInboundHandler.start(updateorder, cptNum);
            j.setSuccess(true);
            j.setMsg("请求更新指令发送成功！");
        } else {
            j.setSuccess(false);
            j.setMsg("请求更新指令错误！");
        }
        return j;
    }

    /**
     * 补偿器模型参数更新
     * 例：Alm,zf00,3300,222
     * @param updateorder  更新指令 SRet_prm
     * @param cptNum       补偿器编号 SCPT-002
     * @param parameter    参数  Alm,zf00,3300,222
     * @return  返回成功标志与提示信息json
     * @throws Exception 异常
     */
    @RequestMapping("/updateparameter")
    @ResponseBody
    public ResponseMsg updateparameter(String updateorder, String cptNum, String parameter) throws Exception {
        ResponseMsg j = new ResponseMsg();
        String udPrmOrder = "SRet_prm";
        if (updateorder.equals(udPrmOrder)) {
            if (cptNum.isEmpty()) {
                j.setSuccess(false);
                j.setMsg("补偿器编号不能为空");
                return j;
            }
            modelUpdateInboundHandler.startParameter(updateorder, cptNum, parameter);
            j.setSuccess(true);
            j.setMsg("请求更新指令发送成功！");
        } else {
            j.setSuccess(false);
            j.setMsg("请求更新指令错误！");
        }
        return j;
    }

}
